/**
 * Created by Administrator on 2016/12/15.
 */

$(function () {
    init();/*初始化*/
});
/*layer弹框效果*/
function ifrLayer (element , url){
    element.on("click", '', function() {
        layer.open({
            type: 2,
            maxmin: false,
            shadeClose: false,
            closeBtn: 1,
            area : ['100%' , '100%'],
            content:url,
            title :　false
        });
    });
}

function init(){
    /*解析href*/
    var str = location.search;
    str = str.slice(1).split('&');
    var json = {};
    for (var i = 0; i < str.length; i++) {
        var a = str[i].split('=');
        json[a[0]] = a[1];
    }
    //console.log(json.projectCode);

    var oUrl = `http://192.168.59.3:8082/console/projectmgt/query/worker/${json.projectCode}`;/*具体某项目的接口*/
    ifrLayer($('#ifr-materiel'),`../materiel-consume/materiel-consume.html?projectCode=${json.projectCode}`);/*物料消耗弹框*/

    /*租户信息获取*/
    var
        oUserNum = $('#userNum'),
        oUserName = $('#userName'),
        oUserId = $('#userId'),
        oSearch = $('#search'),
        oEmpty = $('#empty');
    var
        oNumVal = '',
        oNameVal = '',
        oIdVal = '',
        oData = '';

    $('#workTable').bootstrapTable({
        method : 'post',  //发送方式
        url : oUrl,
        onLoadSuccess : function (row) {
            oSearch.click(oSearchFun);/*点击查询函数*/
            oEmpty.click(oEmptyFun);/*查询条件清空*/
        }
    });

    /*按条件搜索*/
    function oSearchFun(){
        oNumVal = oUserNum.val();
        oNameVal = oUserName.val();
        oIdVal = oUserId.val();

        oData = {
            "idCardNum": oIdVal,
            "userName": oNameVal,
            "userCode": oNumVal
        };

        $.ajax({
            type : 'POST',
            url : oUrl,
            dataType : 'json',
            contentType : 'application/json',
            data:JSON.stringify(oData),
            success : function (data){
                oData1 = responseHandler(data);
                $("#workTable").bootstrapTable('load',oData1);
            },
            error : function (err){
                console.log('错误');
            }
        })
    }
    /*清空条件*/
    function oEmptyFun(){
        oUserNum.val('');
        oUserName.val('');
        oUserId.val('');
        $("#workTable").bootstrapTable('refresh',oUrl);
    }

}


/*回调函数*/
function responseHandler(res) {
    var needData = {
        rows: res.data.list,
        total: res.data.total
    };
    return needData;
}

/*序列号*/
function indexFmt(val,row,index){
    return index+1;
}

/*分页器*/
function workTableQuery(params){
    params["pageNum"] = (params.offset / params.limit) + 1;
    params.pageSize = params.limit;
    return params;
}

function handlerFmt(value,row){
    //console.log(row);
    return `<button onclick="getDetail('${row.projectCode}','${row.teamId}','${row.userCode}','${row.relationId}')"><span class="icon icon-caret-down"></span></button>`
}
function getDetail(code1,code2,code3,code4){
    layer.open({
        type: 2,
        maxmin: false,
        shadeClose: true,
        closeBtn: 1,
        area : ['100%' , '100%'],
        content:`../worker-att-detail/worker-att-detail.html?projectCode=${code1}&teamId=${code2}&userCode=${code3}&relationId=${code4}`,
        title :　false
    });
}